package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IProductTypeDao;
import com.cn.wanxi.model.ProductTypeModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProductTypeDaoImpl implements IProductTypeDao {
    private void clearProductTypeRedis() {
        try {
            Jedis jedis = new Jedis();
            jedis.del("product_type_id");
            jedis.del("product_type_href");
            jedis.del("product_type_name");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<ProductTypeModel> getProductTypeList() {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.IProductTypeDao.getProductTypeList");
    }

    @Override
    public ProductTypeModel getProductType(int id) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.IProductTypeDao.getProductType",id);
    }

    @Override
    public int addProductType(ProductTypeModel productType) {
        clearProductTypeRedis();
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.IProductTypeDao.addProductType",productType);
    }

    @Override
    public int deleteProductType(int id) {
        clearProductTypeRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.IProductTypeDao.deleteProductType",id);
    }

    @Override
    public int updateProductType(ProductTypeModel productType) {
        clearProductTypeRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.IProductTypeDao.updateProductType",productType);
    }

    @Override
    public List<ProductTypeModel> getProductTypes(Map<String,Object> map) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.IProductTypeDao.getProductTypes",map);
    }

    @Override
    public long getProductTypeCount(Map<String,Object> map) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.IProductTypeDao.getProductTypeCount",map);
    }

    @Override
    public int updateShow(ProductTypeModel productType) {
        clearProductTypeRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.IProductTypeDao.updateShow",productType);
    }

    @Override
    public List<ProductTypeModel> getProductTypeListByRedis() {
        Jedis jedis = new Jedis();
        List<ProductTypeModel> list = new ArrayList<>();
        List<String> productTypeId = jedis.lrange("product_type_id",0,-1);
        for (int i = 0;i<productTypeId.size();i++){
            ProductTypeModel productType = new ProductTypeModel();
            productType.setId(Integer.valueOf(productTypeId.get(i)));
            productType.setName(jedis.lindex("product_type_name",i));
            productType.setHref(jedis.lindex("product_type_href",i));
            list.add(productType);
        }
        return list;
    }

    @Override
    public void setProductTypeInRedis(List<ProductTypeModel> list) {
        Jedis jedis = new Jedis();
        for (ProductTypeModel productType:list){
         jedis.rpush("product_type_id", String.valueOf(productType.getId()));
         jedis.rpush("product_type_name",productType.getName());
            jedis.rpush("product_type_href",productType.getHref());
        }
    }
}
